<template>
	<view class="page">
		<image :src="`${staticUrl}/imcdoctorweb/static/img/plate.19f1959.png`" mode="widthFix" class="login-image">
		</image>
		<view class="login-text">
			<text class="title">医共体远程医疗（移动端）</text>
			<text class="text">欢迎使用</text>
		</view>
		<view class="form-container">
			<view class="form-item">
				<u-icon name="account" color="#999" size="24px"></u-icon>
				<input v-model="username" placeholder="请输入账号" class="input"></input>
			</view>
			<view class="form-item">
				<u-icon name="lock" color="#999" size="24px"></u-icon>
				<input v-model="password" placeholder="请输入密码" type="password" class="input"></input>
			</view>
		</view>

		<view class="login-button-warpper">
			<u-button size="large" type="primary" text="登录" shape="circle" :loading='loading' @click="onLogin"></u-button>
		</view>
		<view class="register" @click="toRegister">
			<text style="color: #2961b8;font-size: 16px;"> 注册账号 </text>
		</view>
		<view class="register" @click="onNavigateTo('/pages/agreement/agreement')">
			<text style="color: #2961b8;font-size: 16px;">《用户服务协议》和《隐私政策》 </text>
		</view>
	</view>
</template>

<script>
	import config from '@/common/config.js'
	import {
		loginApi,
		getLoginInfoApi
	} from '@/common/api/index.js'

	const app = getApp()

	export default {
		data() {
			return {
				// username: '', 
				// password: '',
				username: '15200000001', //18800000036  15200000001
				password: '123456',
				loading:false
			}
		},
		onShow() {
			let doctorId = uni.getStorageSync('doctorId');
			if (doctorId && doctorId !== '') {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		},
		methods: {
			onLogin() {
				this.loading=true
				loginApi({
					phisname: this.username,
					password: this.password
				}, {
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					}
				}).then(response => {
					this.getLoginInfo()
					this.loading=false
				}).catch(err => {
					console.log('err', err)
					this.loading=false
				})
			},
			getLoginInfo() {
				getLoginInfoApi().then(res => {
					const {
						doctorId,
						hospitalId,
						hospitalName,
						name,
						doctorPhone,
						deptId,
						deptName,
						orgCode
					} = res.result.user
					uni.setStorageSync('doctorId', doctorId);
					uni.setStorageSync('hospitalId', hospitalId);
					uni.setStorageSync('doctorName', name);
					uni.setStorageSync('hospitalName', hospitalName);
					app.globalData.meetingWebSocket.init(config.meeting_websocke_url + doctorId)
					uni.setStorageSync('doctorPhone', doctorPhone);
					uni.setStorageSync('deptId', deptId);
					uni.setStorageSync('deptName', deptName);
					uni.setStorageSync('orgCode', orgCode);
					uni.switchTab({
						url: '/pages/index/index'
					})
				})
			},
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.login-image {
		margin: 0 auto;
		display: block;
		width: 600rpx;
		max-width: 750px;
	}

	.login-text {
		margin: 30px 0 10px;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.title {
			font-size: 18px;
			font-weight: bold;
			color: #333;
		}

		.text {
			font-size: $ft-size-base;
			color: #999;
		}
	}

	.form-container {
		margin: 0 auto;
		width: 600rpx;
		max-width: 686px;

		.form-item:first-child {
			margin-top: 0;
		}

		.form-item {
			margin-top: 20px;
			padding: 0 32rpx;
			width: 100%;
			height: 50px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			border-radius: 10000px;
			background-color: #fff;

			.input {
				flex: 1;
				margin-left: 5px;
				height: 100%;
				font-size: 16px;
				color: #333;
			}
		}
	}

	.login-button-warpper {
		margin: 20px auto;
		width: 600rpx;
		max-width: 686px;
	}
</style>